package com.hkws.hkws.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hkws.hkws.API.DoorResouceApi;
import com.hkws.hkws.mapper.DoorlistMapper;
import com.hkws.hkws.pojo.AcsDoorListRequest;
import com.hkws.hkws.pojo.Doorlist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;


@Component
public class QueryDoorDot {
    @Autowired
    private DoorlistMapper doorlistMapper;
    private Object o;
  // @Scheduled(initialDelay = 15, fixedRate = 10000)
    public void execute() {
        DoorResouceApi doorResouceApi = new DoorResouceApi();
        AcsDoorListRequest acsDoorListRequest = new AcsDoorListRequest();
        Doorlist doorlist = new Doorlist();
        acsDoorListRequest.setPageNo(1);
        acsDoorListRequest.setPageSize(12);

        /**
         * 查询门禁点列表
         */

        String s = doorResouceApi.acsDoorList(acsDoorListRequest);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");

        for (int i = 0; i < list.size(); i++) {
            JSONObject json1 = (JSONObject) list.get(i);
            System.out.println("json"+"第几"+i+json1);
            Field[] fields = doorlist.getClass().getDeclaredFields();
            for (Field field : fields) {
                // System.out.println(field.getName());
                Iterator<String> iterator = json1.keySet().iterator();
                while (iterator.hasNext()) {
                    String name = iterator.next();
                    if (field.getName().equals(name)) {
                        Object o = json1.get(name);
                        if (field.getType().toString().equals("class java.lang.String")) {
                            String o1;
                            if(o==null)
                            {
                                o1="";

                            }else
                            {
                              o1 = o.toString();
                            }
                            try {
                                field.setAccessible(true);
                                field.set(doorlist, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }else if(field.getType().toString().equals("class java.lang.Integer"))
                        {
                            Integer o1=null;
                            o1=Integer.valueOf(o.toString());
                            try {
                                field.setAccessible(true);
                                field.set(doorlist, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        else {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                            Date ts = null;
                            try {
                                ts = sdf.parse((String) o);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            System.out.println(o);
                            try {
                                field.setAccessible(true);
                                field.set(doorlist, ts);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }

                    }

                }

            }
            System.out.println(doorlist);
            doorlistMapper.insert(doorlist);
        }
    }
}
